<template>
	<div class="llf_content">
		<SearchHeader @search="onSearch" :selectform="selectform" placeholder="患者姓名,患者电话搜索">
			<template v-slot:rightBtn>
				<MyButton class="primary" @click="TableOperation('look')"  title="新增患者" />
				<MyButton class="delete" @click="TableOperation('del')"  title="批量删除" />
			</template>
		</SearchHeader>
		<PatientList :tableData="dataSource" @operation="TableOperation" ref="patinetRef" />
		<MyPagination :total="total" @change="onPaginationChange" ref="pageRef" />
		<!-- 新增患者信息 -->
		<MyDialog v-model:visible="createVisible" :title="currentPatientInfo?.id ? '编辑患者' : '新增患者'" width="50%"
			destroyOnClose>
			<template v-slot:content>
				<crateDlg :currentPatientInfo="currentPatientInfo" @onReset="createVisible = false" 
					@onSubmit="onSubmit"
				/>
			</template>
		</MyDialog>
		<MyDialog v-model:visible="devdVisible" title="分配项目" width="60%"
			destroyOnClose>
			<template v-slot:content>
				<deveItem @onReset="devdVisible = false" @onSubmit="ondevSubmit" />
			</template>
		</MyDialog>
	</div>
</template>

<script lang="ts" setup>
	import { onMounted, ref, computed , watch } from "vue";
	import {useRouter,useRoute} from 'vue-router';
	import { useOrganization } from "@/store";
	import dayjs from "dayjs";
	import { getList, assignItem, deleteFn } from "@/api/patient";
	import options from "@/common";
	import message from "@/utils/message";
	import debounce from "@/utils/debounce";
	import PatientList from "./component/PatientList.vue";
	import crateDlg from "./component/cratePatient.vue";
	import deveItem from "./component/deveItem.vue";
	
	const ROUTER = useRouter();
	const OrganStore = useOrganization();
	OrganStore.getDoctorList();
	
	const doctorList = computed(()=> OrganStore.$state.doctorList);
	const selectform = ref<any>([
		{ title: '关联医生', childer:[], width: '50px', contWidth: '250px',class:'doctorId' },
		{ title: '性别', childer: options.SEX_OPTIONS, width: '50px',class:"sex" },
	]);
	const dataSource = ref([]);
	const patinetRef = ref();
	const total = ref(0);
	const createVisible = ref(false);
	const devdVisible = ref(false);
	const currentPatientInfo = ref({});
	const searchData = ref({ page:1,size:20 });
	const pageRef = ref();
	
	async function getFn(){
		const res = await getList(searchData.value);
		if(!res) return;
		// console.log(res);
		// const { size, list } = res;
		total.value = res.total;
		dataSource.value = res.list;
	}
	
	const onSearch = (inputValue: string, obj: any) => {	//搜索---子组件
		let SEARCH_VALUE = {};
		SEARCH_VALUE = obj;
		SEARCH_VALUE["userName"] = inputValue;
		console.log(SEARCH_VALUE, "SEARCH_VALUE")
		searchData.value.page = 1;
		pageRef.value.changeCurrent();
		searchData.value = { ...searchData.value, ...SEARCH_VALUE };
		getFn();
	};
	
	const onPaginationChange = (value: { page: number; pageSize: number }) => { // 页码与页面数量
		const { pageSize, page } = value;
		// searchData.value = { ...searchData.value, page, limit: pageSize };
		// onGetPatientList();
	};
	
	const cratePatient = ()=>{
		createVisible.value = !createVisible.value;
	};
	
	const onSubmit = debounce(()=>{
		createVisible.value = !createVisible.value;
		getFn();
	}, 50);
	
	const ondevSubmit = debounce(async (row)=>{
		const { data, timer } = row;
		if(data.length == 0) return message.error('请选择量表');
		if(timer.length == 0) return message.error('请选择有效时间');
		const [start, end] = timer;
		const effectiveStart = dayjs(start).format('YYYY-MM-DD'); // 开始时间
		const effectiveEnd = dayjs(end).format('YYYY-MM-DD');
		const qyUserItems = data.map((item)=> ({ 
			itemId:item.id,itemTitle:item.name,visit:item.show?1:0,needCount:item.sorce ,
		}));
		const res = await assignItem({ qyUserItems,userId:currentPatientInfo.value.id,effectiveStart,effectiveEnd });
		if(!res) return;
		message.success('分配成功');
		devdVisible.value = false;
	}, 50);
	
	const TableOperation = async (index: string, row: any = {}) => { // 表格操作
		currentPatientInfo.value = row;
		switch(index){
			case 'look':cratePatient();break;
			case 'dev':devdVisible.value = true;break;
			case 'report':{
				// ROUTER.replace({path: '/workbenches'});
			};break;
			case 'del':{
				if(patinetRef.value.selectedRowKeysval.length == 0) return message.error('请选择需要删除的患者');
				const res = await deleteFn({ ids:patinetRef.value.selectedRowKeysval });
				// ROUTER.replace({path: '/workbenches'});
			};break;
			default:console.log(1);
		}
	};
	
	watch(()=>doctorList.value,()=>{
		selectform.value[0].childer = doctorList.value;
	},{ immediate:true });
	
	onMounted(()=>{
		getFn();
	});
</script>

<style>
</style>